const pug = require('gulp-pug')
const gulp = require('gulp')
const stylus = require('gulp-stylus')
const rename = require("gulp-rename")
const babel = require('gulp-babel')
const error = require('gulp-plumber')

gulp.task('default', ['views', 'style', 'js'])

const pugWatcher = gulp.watch('./pugs/**/*.pug', ['views'])
pugWatcher.on('change', function(event) {
  console.log('File ' + event.path + ' was ' + event.type + ', running tasks...')
})

const styleWatcher = gulp.watch('./stylus/**/*.styl', ['style'])
styleWatcher.on('change', function(event) {
  console.log('File ' + event.path + ' was ' + event.type + ', running tasks...')
})

const jsWatcher = gulp.watch('./es/**/*.js', ['js'])
jsWatcher.on('change', function(event) {
  console.log('File ' + event.path + ' was ' + event.type + ', running tasks...')
})

gulp.task("js", function () {
  return gulp.src("es/**/*.js")
    .pipe(error())
    .pipe(babel({
      presets: ['env']
    }))
    .pipe(gulp.dest("js"))
    .on('error', err => console.log(err))
})

gulp.task('views', function () {
  return gulp.src('./pugs/pages/*.pug')
    .pipe(error())
    .pipe(pug({
      pretty: true,
    }))
    .pipe(gulp.dest('./html'))
})

gulp.task('style', function () {
  return gulp.src('./stylus/index.styl')
    .pipe(error())
    .pipe(stylus())
    .pipe(rename('common.css'))
    .pipe(gulp.dest('./css'))
})